package com.supermap.localization.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.oro.text.regex.MalformedPatternException;

import com.supermap.localization.util.MatcherUtil;

public class Extracter {
    private String beforeExclude;
    private String include;
    private String afterExclude;

    public void setBeforeExclude(String beforeExclude) {
        this.beforeExclude = beforeExclude;
    }

    public void setInclude(String include) {
        this.include = include;
    }

    public void setAfterExclude(String afterExclude) {
        this.afterExclude = afterExclude;
    }

    public List<String> extract(String str) throws MalformedPatternException {
        if (StringUtils.isNotEmpty(beforeExclude)) {
            str = MatcherUtil.exclude(str, beforeExclude);
        }
        List<String> result = new ArrayList<>();
        if (StringUtils.isNotEmpty(include)) {
            result = MatcherUtil.include(str, include);
        }
        if (StringUtils.isNotEmpty(afterExclude)) {
            for (int i = 0; i < result.size(); i++) {
                result.set(i, MatcherUtil.exclude(result.get(i), afterExclude));
            }
        }
        return result;
    }

    public String getStringAfterExtract(String str) throws MalformedPatternException {
        if (beforeExclude != null) {
            str = MatcherUtil.exclude(str, beforeExclude);
        }
        if (include != null) {
            str = str.replaceAll(include, "");
        }
        return str;
    }
}
